package com.southpark.demo.control.commands.user
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.southpark.demo.control.events.user.UserLoggedInEvent;
	import com.southpark.demo.control.events.user.UserLoggedOutEvent;
	import com.southpark.demo.control.events.user.UserLoginEvent;
	import com.southpark.demo.control.proxies.user.UserLoginProxy;
	import com.southpark.demo.control.responders.BaseResponder;
	import com.southpark.framework.control.commands.CommandFault;
	import com.southpark.system.enum.CookieNameEnum;
	

	public class UserLoginCommand extends BaseResponder
	{
		private var event:UserLoginEvent;
		
		override public function execute(cairngormEvent:CairngormEvent):void
		{
			super.execute(cairngormEvent);
			event = cairngormEvent as UserLoginEvent;
			
			var proxy:UserLoginProxy = new UserLoginProxy(this);
			proxy.execute(event.userName,event.password);
		}
		

		override public function onSuccess(result:Object):void
		{
			model.sessionId = result as String;
			model.user.userName = event.userName;
			
			if (event.rememberUser)
        	{
        		applicationManager.cookieManager.setCookie(CookieNameEnum.USER_NAME,event.userName);
        		applicationManager.cookieManager.setCookie(CookieNameEnum.USER_PASSWORD,event.password);
        	}
        	else
        	{
        		applicationManager.cookieManager.setCookie(CookieNameEnum.USER_NAME,'');
        		applicationManager.cookieManager.setCookie(CookieNameEnum.USER_PASSWORD,'');
        	};
			
			new UserLoggedInEvent().dispatch();
			
			super.onSuccess(result);
		}
		
		override public function onFault(commandFault:CommandFault):Boolean
		{
			model.sessionId = null;
			model.user.userName = null;
			
			new UserLoggedOutEvent().dispatch();
			
			return super.onFault(commandFault);
		}
		
	}
}